<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc'
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['fromuser']);
			$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'只有【区品牌店】【市品牌运营中心】才能后台指定区域','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'agent','title'=>'上级','type'=>'member'),
			array('name'=>'level_id','title'=>'品牌代理等级','type'=>'select','options'=>'levels'),
			array('name'=>'a_fee','title'=>'广告值','type'=>'string'),
			array('name'=>'a_sffee','title'=>'可提现广告值','type'=>'string'),
			array('name'=>'g_fee','title'=>'贡献值','type'=>'string'),
			array('name'=>'g_sffee','title'=>'可提现贡献值','type'=>'string'),
			array('name'=>'status','title'=>'品牌代理状态','type'=>'radio','options'=>'agent_status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'set_fee','title'=>'充值'),
			array('type'=>'modal','op'=>'setlevel','title'=>'设置等级'),
			//array('type'=>'modal','op'=>'add','title'=>'设置代理区域','show'=>array('level_type'=>array(4,5))),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'全部等级');
	exi($return);
}
if($_RQ['op']=='set_fee'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$type=intval($post['type']);
		$fee=intval($post['fee']);
		$is_add=intval($post['is_add']);
		if(!$fee){
			exi('请填写操作额度，必须是整数','error');
		}
		if($type){
			if($is_add){
				$this->model('member')->member_feelog($post['mid'],'g_fee',$fee,$is_add,10);
			}else{
				$this->model('member')->member_feelog($post['mid'],'g_fee',$fee,$is_add,11);
			}
		}else{
			if($is_add){
				$this->model('member')->member_feelog($post['mid'],'a_fee',$fee,$is_add,8);
			}else{
				$this->model('member')->member_feelog($post['mid'],'a_fee',$fee,$is_add,9);
			}
		}
		exi('操作成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定品牌代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'充值',
		'edit'=>array(
			array('name'=>'type','title'=>'充值类型','type'=>'radio','options'=>'types'),
			array('name'=>'is_add','title'=>'充值方向','type'=>'radio','options'=>'is_add'),
			array('name'=>'fee','title'=>'额度','type'=>'string'),
		),
	);
	$return['types']=array('广告值','贡献值');
	$return['is_add']=array('减少','增加');
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('member')->check_agent($apply_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定品牌代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核品牌代理申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'agent_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['agent_status']=$this->agent_status;
	exi($return);
}
if($_RQ['op']=='add'){
	//选择代理地区
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['pcd']){
			exi('请先选择代理地区~','error');
		}
		$area_code=$this->model()->get_area_code($post['pcd'],$post['level_id']);	
		$agent_id=$this->model('member')->up_agent($post['mid'],$area_code,$post);
		$this->model('member')->check_agent($agent_id,1);
		exi('设置品牌代理成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定品牌代理不存在！','error');
	}
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置品牌代理',
		'edit'=>array(
			array('type'=>'pcd','name'=>'pcd','title'=>'选择代理地区'),
		),
	);
	exi($return);
}
if($_RQ['op']=='setlevel'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$update=array(
			'level_id'=>intval($post['level_id']),
		);
		$this->update('member',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}
	$item=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定品牌代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置代理商等级',
		'edit'=>array(
			array('name'=>'level_id','title'=>'品牌代理等级','type'=>'select','options'=>'levels'),
		),
	);
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels'][0]=array('id'=>0,'title'=>'全部等级');
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$mid=$this->getcolumn('member',array('id'=>$id),'mid');
		$this->delete('member',array('id'=>$id));
		$this->delete('member',array('mid'=>$mid));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$mid=$this->getcolumn('member',array('id'=>$id),'mid');
				$this->delete('member',array('id'=>$id));
				$this->delete('member',array('mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>